package com.qbm.association.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qbm.association.entity.Activity;
import com.qbm.association.entity.AdministratorS;
import com.qbm.association.entity.AdministratorT;
import com.qbm.association.mapper.ActivityMapper;
import com.qbm.association.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Aron
 * @since 2021-12-12
 */
@RestController
@RequestMapping("/association/activity")
public class ActivityController {
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    ActivityService activityService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Activity> getAllByIdS() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        int originator = Integer.parseInt((String) request.getAttribute("userName"));
        return activityMapper.selectList(new QueryWrapper<Activity>().eq("originator", originator));
    }

    @RequestMapping(value = "/getTeacherActivity", method = RequestMethod.GET)
    public List<Activity> getAllByIdT(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        int administrator = Integer.parseInt((String) request.getAttribute("userName"));
        return activityMapper.selectList(new QueryWrapper<Activity>().eq("administrator", administrator));
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insertActivity(@RequestBody Map<String, Object> para) throws JsonProcessingException, ParseException {

        activityService.add(para);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("state", 1);
        ObjectMapper objectMapper = new ObjectMapper();
        this.getAllByIdS();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteActivity", method = RequestMethod.GET)
    @ResponseBody
    public String deleteActivity(long id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        Activity activity = new Activity();
        activity.setId(id);
        activity.setState(-2);
        int i = activityMapper.updateById(activity);
        this.getAllByIdS();
        if (i != 0) {
            hs.put("state", 0);
        } else {
            hs.put("state", 1);
        }
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/rejectActivity", method = RequestMethod.GET)
    @ResponseBody
    public String rejectActivity(long id) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setState(-1);
        activityMapper.updateById(activity);
        return "";
    }

    @RequestMapping(value = "/agreesActivity", method = RequestMethod.GET)
    @ResponseBody
    public String agreesActivity(long id) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setState(1);
        activityMapper.updateById(activity);
        return "";
    }

}

